<template>
  <!--发布课题-->
  <div class="release-subject">
    <InnerBreadcrumb v-model="indexKey" :path="breadPath" :showBack="true" />

    <div class="edu-page-warp" v-show="indexKey === 'MAIN'">
      <condition-card @reset="reset" @search="searchList" label-width="110px">
        <el-row>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.graduationYear')">
              <el-date-picker class="search-item" :clearable="false" v-model="queryForm.grade" @change="handleGradeChange" value-format="yyyy" type="year" :placeholder="$t('graduationDesign.pleaseChoose')">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.subjectCollege')">
              <el-select v-model="queryForm.collegeId" @change="handleCollegeChange" filterable class="search-item" clearable>
                <el-option v-for="item in source.collegeOptions" :key="item.collegeId" :label="item.collegeIdI18n" :value="item.collegeId" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.subjectMajor')">
              <el-select v-model="queryForm.subjectId" filterable class="search-item" clearable>
                <el-option v-for="item in source.subjectOptions" :key="item.subjectId" :label="item.subjectName" :value="item.subjectId" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.subjectType')">
              <el-select v-model="queryForm.projectType" filterable class="search-item" clearable>
                <el-option v-for="item in source.subjectTypeOptions" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.releaseStatus')">
              <el-select v-model="queryForm.publish" filterable class="search-item" clearable>
                <el-option v-for="item in source.releaseStatusOptions" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.subjectName')">
              <el-input v-model="queryForm.projectName" class="search-item" :placeholder="$t('graduationDesign.pleaseEnter')"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.declarer')">
              <el-input v-model="queryForm.applicantId" class="search-item" :placeholder="$t('graduationDesign.pleaseEnter')"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </condition-card>
      <el-card shadow="never">
        <div class="edu-page-title">{{$t('graduationDesign.releaseSubject')}}</div>
        <div class="edu-opertion-bar">
          <div class="float-right">
            <el-button @click="onExport" v-loading.fullscreen.lock="loading" element-loading-text="Loading" element-loading-spinner="el-icon-loading" element-loading-background="rgba(255, 255, 255, .48)">{{$t('graduationDesign.export')}}</el-button>
            <el-button type="primary" @click="onRelease($event, 'BATCH')" v-premit="release_subject">{{$t('graduationDesign.release')}}</el-button>
          </div>
        </div>
        <edu-table ref="relesaeSubjectTable" :url="getList" @selection-change="handleSelectChange">
          <edu-table-column :label="$t('graduationDesign.graduationYear')" prop="grade" min-width="90"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.subjectName')" prop="projectName" min-width="100"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.declarer')" prop="applicantName" min-width="90"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.subjectCollege')" prop="collegeIdI18n" min-width="120"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.subjectMajor')" prop="subjectIdI18n" min-width="120"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.subjectType')" prop="projectType" min-width="100"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.reviewer')" prop="approvalName" min-width="90"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.status')" min-width="100">
            <template slot-scope="scope">
              <el-tag type="success" v-if="scope.row.publish">{{$t('graduationDesign.published')}}</el-tag>
              <el-tag type="info" v-else>{{$t('graduationDesign.unpublished')}}</el-tag>
            </template>
          </edu-table-column>
          <edu-table-column :label="$t('graduationDesign.operation')" min-width="100">
            <template slot-scope="scope">
              <el-button type="text" @click="onRelease(scope.row)" v-if="!scope.row.publish" v-premit="release_subject">{{$t('graduationDesign.release')}}</el-button>
              <el-button type="text" @click="onView(scope.row)">{{$t('graduationDesign.view')}}</el-button>
            </template>
          </edu-table-column>
        </edu-table>
      </el-card>
    </div>

    <div v-if="indexKey === 'VIEW'">
      <TopicInfo :actionType="actionType" :basicInfoForm="basicInfoForm" :progressData="progressData"></TopicInfo>
    </div>
  </div>
</template>
<script>
import API from "common/src/api/underGraduationDesign/releaseSubject";
import tools from 'common/src/views/underGraduationDesign/tools.js';
export default {
  name: 'releaseSubject',
  data() {
    return {
      api: API,
      getList: '/api/designservice/optApproval/publishList',
      queryForm: {
        grade: '',
        collegeId: '',
        subjectId: '',
        projectType: '',
        projectName: '',
        publish: '',
        applicantName: '',
        authId: localStorage.getItem('activeAuthId')
      },
      indexKey: 'MAIN',
      source: {
        collegeOptions: [], // 学院
        subjectOptions: [], // 专业
        // 课题类型
        subjectTypeOptions: [{
          label: "毕业论文",
          value: "毕业论文",
        }, {
          label: "毕业设计",
          value: "毕业设计",
        }],
        releaseStatusOptions: [{
          label: "已发布",
          value: true
        }, {
          label: "未发布",
          value: false
        }]
      },
      releaseSelectedData: [], // 勾选的列表数据
      breadPath: {
        label: this.$t('graduationDesign.releaseSubject'),
        key: "MAIN",
        children: [
          {
            key: "VIEW",
            label: this.$t('graduationDesign.details')
          }
        ]
      },
      loading: false,
      actionType: "",
      basicInfoForm: {},
      progressData: [] //审核进度列表
    };
  },
  mounted() {
    this.queryForm.grade = new Date().getFullYear() + '';
    this.getCollege();
    this.searchList();
  },
  methods: {
    searchList() {
      this.$refs.relesaeSubjectTable.search(this.queryForm);
    },
    reset() {
      this.queryForm = {
        grade: '',
        collegeId: '',
        subjectId: '',
        projectType: '',
        projectName: '',
        publish: '',
        applicantName: '',
        authId: localStorage.getItem('activeAuthId')
      };
      this.queryForm.grade = new Date().getFullYear() + '';
      this.source.subjectOptions = [];
      this.searchList();
    },
    handleSelectChange(val) {
      this.releaseSelectedData = val;
    },
    handleGradeChange(val) {
      this.queryForm.subjectId = "";
      this.source.subjectOptions = [];
      if (val && this.queryForm.collegeId) {
        this.handleCollegeChange(this.queryForm.collegeId);
      }
    },
    // 查学院
    getCollege() {
      this.api.getCollege().then(res => {
        this.source.collegeOptions = res;
      });
    },
    // 选择年份和学院查专业
    handleCollegeChange(val) {
      this.queryForm.subjectId = "";
      this.source.subjectOptions = [];
      if (val && this.queryForm.grade) {
        const params = {
          grade: this.queryForm.grade,
          college: val
        };
        this.api.getSubject(params).then(res => {
          this.source.subjectOptions = res.data;
        }).catch();
      }
    },
    // 查看
    onView(row) {
      this.actionType = "VIEW_STU";
      // "infoType"： 1 老师，0 学生
      if (row.infoType === "1") {
        this.actionType = "VIEW_TUTOR";
      }
      const params = { ...row };
      this.api.getSubjectInfo(params).then(res => {
        this.indexKey = 'VIEW';
        this.basicInfoForm = { ...res.data };
        this.basicInfoForm.grade = res.data.grade + "";
        this.progressData = [];
        if (row.flowNo) {
          this.getReviewProgress(row.flowNo);
        }
      }).catch();
    },
    // 查审核进度
    getReviewProgress(applyNo) {
      this.api.getProgress(applyNo).then(res => {
        this.progressData = res.data.listApproveHistory;
      }).catch();
    },
    // 发布
    onRelease(row, type) {
      if (type === "BATCH" && !this.releaseSelectedData.length) {
        this.$eduAlert(this.$t('graduationDesign.pleaseSelectData'), this.$t('graduationDesign.prompt'));
        return;
      }
      this.$eduConfirm(this.$t('graduationDesign.publishConfirmTips'), this.$t('graduationDesign.release')).then(() => {
        const params = type === "BATCH" ? this.releaseSelectedData : [{ ...row }];
        this.api.release(params).then(res => {
          this.$message.success(this.$t('graduationDesign.releaseSuccess'));
          this.searchList();
        }).catch();
      });
    },
    // 导出
    onExport() {
      const params = Object.assign(this.queryForm, { exprotList: this.releaseSelectedData });
      this.api.export(params).then(res => {
        if (res.code == 200) {
          // this.delayRequest(res.data.key);
          tools.downloadFile(res.data.key, "发布课题.xls", null, this);
        }
      });
    },
    // 查文件导出状态，等待导出
    async delayRequest(key) {
      this.loading = true;
      await new Promise(resolve => setTimeout(resolve, 3000));
      this.api.getExportStatus(key).then(res => {
        this.loading = false;
        if (res.code === 200 && res.data.status) {
          let path = 'api/designservice/common/export?path=' + res.data.path;
          window.open(path);
        } else {
          this.delayRequest(key);
        }
      }).catch(err => {
        this.loading = false;
        this.delayRequest(key);
      });
    }
  },
  components: {
    TopicInfo: () => import('../components/topicInfo')
  }
}
</script>
<style lang="scss" scoped>
.release-subject {
    .search-item {
        width: 100%;
    }
}
</style>
